<?php

autoLoader();

$strAction = ( true == isset( $_GET['action'] ) && true == valStr( $_GET['action'] ) ) ? $_GET['action'] : NULL;

switch( strtolower( trim( $strAction ) ) ) {
	case 'searchpatient':
		searchPatientDetails();
		break;

	case 'serachpatientbycasepaper':
		serachPatientWithCasePaper();
		break;

	case 'delete':
		handleDeleteData();
		break;

	case 'quick_insert':
		handleQuickInsertData();
		break;

	default:
		display( 'Provided action not found' );
		exit;
		break;
}
exit;

function searchPatientDetails() {

	$objPatient 	= new Patients();
	$arrobjPatients = $objPatient->fetchAllPatients();
	
	$page 	= $_GET['page']; // get the requested page
	$limit 	= $_GET['rows']; // get how many rows we want to have into the grid
	$sidx 	= $_GET['sidx']; // get index row - i.e. user click to sort
	$sord 	= $_GET['sord']; // get the direction
	
	$searchTerm = $_GET['searchTerm'];
	
	if( !$sidx ) $sidx = 1;
	if( $searchTerm == "" ) {
		$searchTerm = "%";
	} else {
		$searchTerm = "%" . $searchTerm . "%";
	}
	
	if( true == valArr( $arrobjPatients ) ) {
		$count = getPatientTotalCount( $strTableName = 'patients' );
	}
	
	if( $count > 0 ) {
		$total_pages = ceil( $count / $limit );
	} else {
		$total_pages = 0;
	}
	if( $page > $total_pages ) $page = $total_pages;
	
	$start = $limit * $page - $limit; // do not put $limit*($page - 1)
	
	if( $total_pages != 0 ) {
		$strSql = "SELECT * FROM patients WHERE first_name like '$searchTerm'  ORDER BY $sidx $sord LIMIT $start , $limit";
		
		$arrobjPatients = $objPatient->fetchSql( $strSql );
	} else {
		$strSql = "SELECT * FROM patients WHERE first_name like '$searchTerm'  ORDER BY $sidx $sord";
		
		$arrobjPatients = $objPatient->fetchSql( $strSql );
	}
	
	
	
	$response->page 	= $page;
	$response->total 	= $total_pages;
	$response->records 	= $count;
	
	$i=0;
	
	$response->rows[$i]['id'] 			= 0;
	$response->rows[$i]['name'] 		= 'Not Found';
	$response->rows[$i]['Occupation']	= '';
	
	if( true == valArr( $arrobjPatients ) ) {
		foreach( $arrobjPatients as $objPatient ) {
			/*
			 $response->rows[$i]['id']=$row[id];
			$response->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
			*/
			$response->rows[$i]['id'] 		= $objPatient->getId();
			$response->rows[$i]['name'] 	= $objPatient->getFirstName() . ' ' . $objPatient->getLastName();
			$response->rows[$i]['Occupation']	= $objPatient->getOccupaction();
			
			//$response->rows[$i]=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
			$i++;
		}
	}

	echo json_encode( $response );
	exit;
}

function serachPatientWithCasePaper() {
	$objPatient 	= new Patients();
	$arrobjPatients = $objPatient->fetchAllPatients();
	
	$page 	= $_GET['page']; // get the requested page
	$limit 	= $_GET['rows']; // get how many rows we want to have into the grid
	$sidx 	= $_GET['sidx']; // get index row - i.e. user click to sort
	$sord 	= $_GET['sord']; // get the direction
	
	$searchTerm = $_GET['searchTerm'];
	
	if( !$sidx ) $sidx = 1;
	if( $searchTerm == "" ) {
		$searchTerm = "%";
	} else {
		$searchTerm = "%" . $searchTerm . "%";
	}
	
	if( true == valArr( $arrobjPatients ) ) {
		$count = getPatientTotalCount( $strTableName = 'patients' );
	}
	
	if( $count > 0 ) {
		$total_pages = ceil( $count / $limit );
	} else {
		$total_pages = 0;
	}
	if( $page > $total_pages ) $page = $total_pages;
	
	$start = $limit * $page - $limit; // do not put $limit*($page - 1)
	
	if( $total_pages != 0 ) {
		$strSql = "SELECT * FROM patients WHERE first_name like '$searchTerm'  ORDER BY $sidx $sord LIMIT $start , $limit";
	
		$arrobjPatients = $objPatient->fetchSql( $strSql );
	} else {
		$strSql = "SELECT * FROM patients WHERE first_name like '$searchTerm'  ORDER BY $sidx $sord";
	
		$arrobjPatients = $objPatient->fetchSql( $strSql );
	}
	
	
	
	$response->page 	= $page;
	$response->total 	= $total_pages;
	$response->records 	= $count;
	
	$i=0;
	
	$response->rows[$i]['id'] 			= 0;
	$response->rows[$i]['name'] 		= 'Not Found';
	$response->rows[$i]['case_paper_id']	= '';
	
	$objCasePaperTemp = new CasePapers();
	
	if( true == valArr( $arrobjPatients ) ) {
		foreach( $arrobjPatients as $objPatient ) {
			
			$strSql = 'SELECT * FROM case_papers WHERE patient_id = ' . $objPatient->getId() . ' ORDER BY id DESC LIMIT 1;';
			$arrobjCasePapers = ( array ) $objCasePaperTemp->fetchSql( $strSql );
			
			$objCasePaper = current( $arrobjCasePapers );
			
			/*
			 $response->rows[$i]['id']=$row[id];
			$response->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
			*/
			
			$response->rows[$i]['id'] 				= $objPatient->getId();
			$response->rows[$i]['name'] 			= $objPatient->getFirstName() . ' ' . $objPatient->getLastName();
			$response->rows[$i]['case_paper_id']	= ( true == valObj( $objCasePaper, 'BaseCasePapers' ) ) ? $objCasePaper->getId() : NULL;
				
			//$response->rows[$i]=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
			$i++;
		}
	}
	
	echo json_encode( $response );
	exit;
}

function getPatientTotalCount( $strTableName ) {
	$objConnection = new Patients();
	
	$strSql = 'SELECT count( id ) as count FROM ' . $strTableName . ';';
	
	$result = $objConnection->connection->RunQuery( $strSql );
	
	$intPatientCount = NULL;
	if( true == $result ) {
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$intPatientCount = $row['count'];
		}
	}
	
	return $intPatientCount;
}

?>
